{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 矩阵\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5356b5f9-27db-4c7d-b9d6-cebfa1dd72ee",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "该代码通过`numpy`库创建了一个矩阵$A$，并展示了如何提取该矩阵的特定列和行元素。\n",
    "\n",
    "1. **矩阵定义**：代码定义了一个$2 \\times 3$的矩阵$A$，内容如下：\n",
    "\n",
    "   $$\n",
    "   A = \\begin{bmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "2. **提取列向量**：\n",
    "   - `A_first_col`提取矩阵的第一列，存储为一维数组（行向量）：\n",
    "     $$\n",
    "     A[:, 0] = [1, 4]\n",
    "     $$\n",
    "   - `A_first_col_V2`以列向量的形式提取第一列：\n",
    "     $$\n",
    "     A[:, [0]] = \\begin{bmatrix} 1 \\\\ 4 \\end{bmatrix}\n",
    "     $$\n",
    "   - `A_first_second_col_V2`提取第一列和第二列：\n",
    "     $$\n",
    "     A[:, [0, 1]] = \\begin{bmatrix} 1 & 2 \\\\ 4 & 5 \\end{bmatrix}\n",
    "     $$\n",
    "   - `A_first_third_col_V2`提取第一列和第三列：\n",
    "     $$\n",
    "     A[:, [0, 2]] = \\begin{bmatrix} 1 & 3 \\\\ 4 & 6 \\end{bmatrix}\n",
    "     $$\n",
    "\n",
    "3. **提取行向量**：\n",
    "   - `A_first_row`提取第一行：\n",
    "     $$\n",
    "     A[[0], :] = \\begin{bmatrix} 1 & 2 & 3 \\end{bmatrix}\n",
    "     $$\n",
    "   - `A_second_row`提取第二行：\n",
    "     $$\n",
    "     A[[1], :] = \\begin{bmatrix} 4 & 5 & 6 \\end{bmatrix}\n",
    "     $$\n",
    "\n",
    "4. **提取特定元素**：`A_second_row_first_col`提取矩阵$A$中第二行第一列的元素，即$4$：\n",
    "\n",
    "   $$\n",
    "   A[[1], [0]] = 4\n",
    "   $$\n",
    "\n",
    "### 总结\n",
    "\n",
    "该代码展示了如何使用`numpy`对矩阵的行和列进行灵活提取，为矩阵处理中的各种数据提取需求提供了实现方法。这种操作广泛应用于数据分析和科学计算中，特别是在对矩阵数据的选择和变换过程中。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c9d9697-22a9-4f88-84b7-10497f3d0ca4",
   "metadata": {},
   "source": [
    "## 使用numpy提取矩阵的不同列和行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "956d0eba-d77e-4c24-af03-68b2ddd34d00",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入numpy库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e8c82a2-475c-4f5d-86b3-0028a17374fa",
   "metadata": {},
   "source": [
    "## 定义矩阵A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b143b4cd-e988-4bec-821d-a64fed58cff6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[1, 2, 3],\n",
    "              [4, 5, 6]])  # 定义2x3矩阵A\n",
    "A  # 输出矩阵A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5f9e49c-b4b4-44d8-9357-15cd0b53ca86",
   "metadata": {},
   "source": [
    "## 提取矩阵的不同列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "17be9457-2747-4233-945d-b9ae93493eae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_first_col = A[:,0]  # 提取矩阵A的第一列（存储为一维行向量）\n",
    "A_first_col  # 输出第一列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "33829f6d-4f17-46ff-9924-b1f8e5178b1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [4]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_first_col_V2 = A[:,[0]]  # 提取矩阵A的第一列（存储为列向量）\n",
    "A_first_col_V2  # 输出第一列（列向量形式）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b8459938-ed4e-4b59-aa40-ed9ce230b9d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [4, 5]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_first_second_col_V2 = A[:,[0,1]]  # 提取矩阵A的第一列和第二列\n",
    "A_first_second_col_V2  # 输出第一列和第二列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1d1abec6-814d-4c16-bf9d-f434b5306a88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3],\n",
       "       [4, 6]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_first_third_col_V2 = A[:,[0,2]]  # 提取矩阵A的第一列和第三列\n",
    "A_first_third_col_V2  # 输出第一列和第三列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1d5d57f-4364-48e8-8a7e-06131d9cada7",
   "metadata": {},
   "source": [
    "## 提取矩阵的不同行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9cfb27c6-daf5-48df-830a-96dba2f4f211",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_first_row = A[[0],:]  # 提取矩阵A的第一行\n",
    "A_first_row  # 输出第一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "917e93c1-a309-4fca-9f94-9261827f2db0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 5, 6]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_second_row = A[[1],:]  # 提取矩阵A的第二行\n",
    "A_second_row  # 输出第二行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9274bc24-b151-4b64-9d56-aa4dda899657",
   "metadata": {},
   "source": [
    "## 提取矩阵A中第二行的第一列元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "229df52f-39e0-4ecf-bd5e-d4067381e2b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_second_row_first_col = A[[1],[0]]  # 提取矩阵A中位置(i=2, j=1)的元素\n",
    "A_second_row_first_col  # 输出矩阵A中第二行第一列的元素"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
